home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / STREAMS.SWG / 0005_A streaming method for EXE's.pas < prev    next >
Pascal/Delphi Source File  |  1995-02-28  |  3KB  |  131 lines

  1. {
  2. I conceived and made something I find highly useful, hope you do to.. If you
  3. use this in combination with DJ's Streams unit, you can store text files and
  4. that kind of things in your EXE.. On its own, it's great for the usual Config record.
  5. }
  6.  
  7. Unit ExeS;
  8.  
  9. INTERFACE
  10.  
  11. Uses
  12.   Objects;
  13.  
  14. Type
  15.   PExeStream = ^TExeStream;
  16.   TExeStream = Object(TBufStream)
  17.     Constructor Init(FileName: FNameStr; Mode, Size: Word);
  18.     Procedure Seek(Pos: Longint); Virtual;
  19.     Function GetPos: Longint; Virtual;
  20.     Function GetSize: Longint; Virtual;
  21.   Private
  22.     AddOffset: LongInt;
  23.   End;
  24.  
  25. Implementation
  26.  
  27. Constructor TExeStream.Init(FileName: FNameStr; Mode, Size: Word);
  28.  
  29.   Type
  30.     ExeHdrType = Record
  31.     Signature: Word;
  32.     Rest, Blocks: Word;
  33.   End;
  34.  
  35.   Var
  36.     ExeFil: File Of ExeHdrType;
  37.     ExeHdr: ExeHdrType;
  38.     ExeLen: LongInt;
  39.     fm: Integer;
  40.   
  41.   Begin
  42.     fm := FileMode;
  43.     FileMode := 64;
  44.     System.Assign(ExeFil, FileName);
  45.     System.Reset(ExeFil);
  46.     System.Read(ExeFil, ExeHdr);
  47.     System.Close(ExeFil);
  48.     FileMode := fm;
  49.     Inherited Init(FileName, Mode, Size);
  50.     AddOffset := (ExeHdr.Blocks - 1) * LongInt(512) + ExeHdr.Rest;
  51.     Seek(0);
  52.   End;
  53.  
  54. Procedure TExeStream.Seek(Pos: Longint);
  55.  
  56.   Begin
  57.     Inherited Seek(Pos + AddOffset);
  58.   End;
  59.  
  60. Function TExeStream.GetPos: Longint;
  61.  
  62.   Var
  63.     p: LongInt;
  64.  
  65.   Begin
  66.     p := Inherited GetPos;
  67.     GetPos := p - AddOffset;
  68.   End;
  69.  
  70. Function TExeStream.GetSize: Longint;
  71.  
  72.   Var
  73.     s: LongInt;
  74.  
  75.   Begin
  76.     s := Inherited GetSize;
  77.     GetSize := s - AddOffset;
  78.   End;
  79.  
  80. End.
  81.  
  82. { -------------------   DEMO PROGRAM -----------------------}
  83. Below is a simple example program to show its potential use:
  84.  
  85. Program TestExeS;
  86.  
  87. Uses
  88.   ExeS;
  89.  
  90. Type
  91.   TConfig = Record
  92.     Value1: String;
  93.     Value2: Word;
  94.   End;
  95.  
  96. Var
  97.   InS: PStream;
  98.   Config: TConfig;
  99.  
  100. Begin
  101.   InS := New(PExeStream, Init(ParamStr(0), stOpen, 2048));
  102.   If InS = nil Then
  103.     Begin
  104.       Writeln('Something is really wrong!');
  105.       Halt;
  106.     End
  107.   Else If InS^.Status <> stOk Then
  108.     Begin
  109.       Writeln('Something is really wrong!');
  110.       Halt;
  111.     End;
  112.   If InS^.GetSize > 0 Then
  113.     Begin
  114.       InS^.Read(Config, SizeOf(TConfig));
  115.       InS^.Seek(0);
  116.       Writeln('Old config:');
  117.       Writeln('Value 1: ', Config.Value1);
  118.       Writeln('Value 2: ', Config.Value2);
  119.     End
  120.   Else
  121.     Writeln('No config info found.');
  122.   Writeln;
  123.   Write('Enter new value 1: ');
  124.   Readln(Config.Value1);
  125.   Write('Enter new value 2: ');
  126.   Readln(Config.Value2);
  127.   InS^.Write(Config, SizeOf(TConfig));
  128.   Dispose(InS, Done);
  129. End.
  130.  
  131.